<template>
  <el-menu :default-active="activeMenu" mode="vertical" router>
    <div class="p-4 bg-gray-100 border-b">
      <div class="font-medium">当前用户: {{ currentUser.realName || '未登录' }}</div>
      <div class="text-sm text-gray-600">科室: {{ currentDeptName }}</div>
      <div class="text-sm text-gray-600">角色: {{ currentUserType }}</div>
    </div>
    
    <!-- 管理员菜单项 -->
    <template v-if="isAdmin">
      <el-menu-item index="/user">用户管理</el-menu-item>
      <el-menu-item index="/constant-category">常数项管理</el-menu-item>
      <el-menu-item index="/constant-type">常数类型管理</el-menu-item>
    </template>
    
    <!-- 挂号员菜单项 -->
    <el-menu-item index="/registration" v-if="isUser">挂号</el-menu-item>
    
    <!-- 医生菜单项 -->
    <template v-if="isDoctor">
      <el-menu-item index="/medical-record">病案首页登记</el-menu-item>
      <el-menu-item index="/check-apply">开具检查申请</el-menu-item>
      <el-menu-item index="/perform-check">执行检查</el-menu-item>
      <el-menu-item index="/result-check">门诊确诊</el-menu-item>
    </template>
    
    <!-- 通用菜单项 -->
    <el-menu-item index="/logout" @click="onLogout">退出登录</el-menu-item>
  </el-menu>
</template>

<script setup>
import { ref, onMounted, computed } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'

const router = useRouter()
const activeMenu = ref(window.location.pathname)
const isAdmin = ref(false)
const isUser = ref(false)
const isDoctor = ref(false)
const currentUser = ref({})
const currentDeptName = ref('')
const currentUserType = ref('')

// 计算是否为检查科室
const isCheckDepartment = computed(() => {
  const deptID = localStorage.getItem('deptID')
  const checkDepts = ['5', '6', '7', '8', '9', '10'] // 假设这些是检查科室的ID
  return checkDepts.includes(deptID)
})

onMounted(() => {
  const userType = localStorage.getItem('userType')
  isAdmin.value = userType === '1'
  isUser.value = userType === '2'
  isDoctor.value = userType === '3'
  
  const deptMap = {
    '1': '内科',
    '2': '外科',
    '3': '儿科',
    '4': '妇产科',
    '5': '眼科',
    '6': '口腔科',
    '7': '皮肤科',
    '8': '精神科',
    '9': '急诊科',
    '10': '放射科' // 新增检查科室
  }
  
  const deptID = localStorage.getItem('deptID')
  currentDeptName.value = deptMap[deptID] || '未知科室'
  
  const realName = localStorage.getItem('realName')
  currentUser.value = { realName }
  
  // 更新用户类型显示
  if (isAdmin.value) {
    currentUserType.value = '管理员'
  } else if (isDoctor.value) {
    currentUserType.value = '医生'
  } else if (isUser.value) {
    currentUserType.value = '挂号员'
  } else if (isCheckDepartment.value) {
    currentUserType.value = '检查科室'
  } else {
    currentUserType.value = '普通用户'
  }
})

const onLogout = () => {
  localStorage.removeItem('token')
  localStorage.removeItem('userType')
  localStorage.removeItem('deptID')
  localStorage.removeItem('realName')
  ElMessage.success('退出登录成功')
  router.push('/login')
}
</script>